package com.minisoft.ai;// 建议dashscope SDK的版本 >= 2.15.0
import com.alibaba.dashscope.app.*;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.JsonUtils;
import io.reactivex.Flowable;

public class Main3 {
    public static void streamCall() throws NoApiKeyException, InputRequiredException {
        String bizParams =
                "{\"city\":\"杭州\"}";
        ApplicationParam param = ApplicationParam.builder()
                // 若没有配置环境变量，可用百炼API Key将下行替换为：.apiKey("sk-xxx")。但不建议在生产环境中直接将API Key硬编码到代码中，以减少API Key泄露风险。
                .apiKey("sk-837018ca105a4629ac23b8545d3c844a")
                .appId("64a1c6a0457340fdbd58de840e105c79")
                .prompt("我比较看重学区质量，华发有哪些盘比较合适")
                .bizParams(JsonUtils.parse(bizParams))
                .incrementalOutput(false) // 增量输出
                .hasThoughts(true) // 工作流应用实现流式输出需要设置此参数为true，输出结果在thoughts字段中查看
                .build();

        Application application = new Application();
        Flowable<ApplicationResult> result = application.streamCall(param); // 实现流式输出
        result.blockingForEach(data -> {
            String requestId = data.getRequestId();
            System.out.println(requestId);
            System.out.printf("%s\n",data.getOutput().getThoughts());// 处理输出只展示thoughts字段
        });
    }

    public static void main(String[] args) {
        try {
            streamCall();
        } catch (ApiException | NoApiKeyException | InputRequiredException e) {
            System.out.printf("Exception: %s", e.getMessage());
            System.out.println("请参考文档：https://help.aliyun.com/zh/model-studio/developer-reference/error-code");
        }
        System.exit(0);
    }
}